package com.ivy.wechatforclient.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.ivy.wechatforclient.R;
import com.ivy.wechatforclient.utils.HttpUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class LoginActivity extends AppCompatActivity {
    private EditText et_telephone;
    private EditText et_password;
    private CheckBox cb_isSave;
    private SharedPreferences sp ;
    private String path = "http://192.168.49.36:8080/WeChatForServer/UserServlet";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        et_telephone = (EditText) findViewById(R.id.et_telephone);
        et_password = (EditText) findViewById(R.id.et_password);
        cb_isSave = (CheckBox) findViewById(R.id.cb_isSave);

        sp = getSharedPreferences("info",MODE_PRIVATE);

        setSharedPreference();


    }

    Map<String ,String > params = new HashMap<String, String>();
    public void login(View view) {
        final String telephone = et_telephone.getText().toString().trim();
        final String password = et_password.getText().toString().trim();
        params.put("telephone",telephone);
        params.put("password",password);

        getSharedPreference(telephone, password);
        // -------------------------- 处理登陆的逻辑------------------
        Thread loginThread = new Thread(new LoginThread());
        loginThread.start();

    }


    public void register(View view) {
        Intent intent = new Intent(this,RegisterActivity.class);
        startActivityForResult(intent,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null)
            return;
        switch (requestCode){
            case 1:
                et_telephone.setText(data.getStringExtra("telephone"));
                et_password.setText(data.getStringExtra("password"));
                break;

        }
    }

    private void setSharedPreference() {
        boolean isSave = sp.getBoolean("isSave",false);
        if(isSave) {
            et_telephone.setText(sp.getString("telephone",""));
            et_password.setText(sp.getString("password",""));
            cb_isSave.setChecked(true);
        }
    }

    private void getSharedPreference(String telephone,String password) {
        if(TextUtils.isEmpty(telephone) || TextUtils.isEmpty(password)) {
            Toast.makeText(getApplicationContext(),"用户账号或密码不能为空！！",Toast.LENGTH_SHORT).show();
        } else {
            boolean checked = cb_isSave.isChecked();
            SharedPreferences.Editor editor = sp.edit();
            if (checked) {
                editor.putString("telephone",telephone);
                editor.putString("password",password);
            }
            editor.putBoolean("isSave",checked);
            editor.commit();
        }
    }


    class LoginThread implements Runnable{
        public void run(){
            String result = HttpUtil.postByHttpClient(path , params , "login");
            Message msg = new Message();
            msg.obj = result;
            handler.sendMessage(msg);

        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String temp = (String) msg.obj;
            if ("success".equals(temp)){
                Intent intent = new Intent(LoginActivity.this, InitActivity.class);
                String telephone = et_telephone.getText().toString().trim();
                intent.putExtra("owner_tel",telephone);
                startActivity(intent);
            }else if("error".equals(temp)){
                Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_LONG).show();
            }
        }
    };

}